<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-row>
        <el-col :span="24">
          <!-- 这里其实可以进行简化，不允许用户选择上级菜单或菜单类型
          (根据操作的数据——row进行判断即可) -->
          <el-form-item label="上级菜单" prop="parentId">
            <treeselect
              v-model="form.parentId"
              :options="menuList"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级菜单"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="菜单类型" prop="menuType">
            <el-radio-group v-model="form.menuType">
              <el-radio
                v-for="item in menuTypeOptions"
                :key="item.dictCode"
                :label="item.dictValue"
              >
                {{ item.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="form.menuType!==2">
          <el-form-item label="菜单图标" prop="icon">
            <el-popover
              placement="bottom-start"
              width="460"
              trigger="click"
              @show="$refs['iconSelect'].reset()"
            >
              <IconSelect ref="iconSelect" @selected="selected"/>
              <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标" readonly>
                <svg-icon
                  v-if="form.icon"
                  slot="prefix"
                  :icon-class="form.icon"
                  class="el-input__icon"
                  style="height: 32px;width: 16px;"
                />
                <i v-else slot="prefix" class="el-icon-search el-input__icon"/>
              </el-input>
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜单名称" prop="menuName">
            <el-input v-model="form.menuName" placeholder="请输入菜单名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示排序" prop="orderNum">
            <el-input v-model="form.orderNum" placeholder="请输入显示排序码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType!==2">
          <el-form-item prop="path">
              <span slot="label">
                <el-tooltip content="访问的路由地址，如：`user`，如外网地址需内链访问则以`http(s)://`开头" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                路由地址
              </span>
            <el-input v-model="form.path" placeholder="请输入路由地址"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType===1">
          <el-form-item prop="component">
              <span slot="label">
                <el-tooltip content="访问的组件路径，如：`system/user/index`，默认在`views`目录下" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                组件路径
              </span>
            <el-input v-model="form.component" placeholder="请输入组件路径"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType!==0">
          <el-form-item prop="perms">
            <el-input v-model="form.perms" placeholder="请输入权限标识" maxlength="100"/>
            <span slot="label">
                <el-tooltip content="控制器中定义的权限字符，如：@PreAuthorize(`@ss.hasPermi('system:user:list')`)" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                权限标识
              </span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
              <span slot="label">
                <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                显示状态
              </span>
            <el-radio-group v-model="form.visible">
              <el-radio v-for="status in visibleOptions" :key="status.dictValue" :label="status.dictValue">
                {{ status.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
              <span slot="label">
                <el-tooltip content="选择停用则路由将不会出现在侧边栏，也不能被访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                菜单状态
              </span>
            <el-radio-group v-model="form.status">
              <el-radio v-for="status in statusOptions" :key="status.dictValue" :label="status.dictValue">
                {{ status.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :sapn="24" style="text-align: center">
          <el-button type="primary" @click="add">添加</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import sysMenuApi from "@/api/system/sysMenu";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect"
import {emptyStringToNull} from "@/utils/form";

export default {
  name: "sys-menu-add",
  components: {
    Treeselect, IconSelect
  },
  props: {
    activeObj: {
      type: Object,
      default: undefined
    }
  },
  watch: {
    activeObj: {
      immediate: true,
      handler: function (newVal, oldVal) {
        if (newVal) {
          this.form.parentId = newVal.menuId;
          if (newVal.menuType === 0) {
            this.form.menuType = 1
          } else if (newVal.menuType === 1) {
            this.form.menuType = 2
          }
        } else {
          this.form.menuType = 0
          this.form.parentId = 0
        }
      }
    }
  },
  data() {
    return {
      //保存的数据
      form: {
        parentId: null,
        menuType: null,
        menuName: null,
        orderNum: null,
        path: null,
        component: null,
        perms: null,
        icon: null,
        visible: 0,
        status: 0
      },
      // 表单校验
      rules: {
        parentId: [
          {required: true, message: "上级菜单不能为空", trigger: "blur"}
        ],
        menuName: [
          {required: true, message: "菜单名称不能为空", trigger: "blur"}
        ],
        orderNum: [
          {required: true, message: "显示排序不能为空", trigger: "blur"}
        ],
        path: [
          {required: true, message: "路由地址不能为空", trigger: "blur"}
        ]
      },
      //状态字典数据
      statusOptions: [],
      //显示隐藏的字典数据
      visibleOptions: [],
      // 菜单类型字典数据
      menuTypeOptions: [],
      //菜单树的数据
      menuList: []
    }
  },
  created() {
    this.getDataByType("sys_status_type").then(res => {
      this.statusOptions = res.data;
    })
    this.getDataByType("sys_show_hide").then(res => {
      this.visibleOptions = res.data;
    })
    this.getDataByType("sys_menu_type").then(res => {
      this.menuTypeOptions = res.data
    })
    this.loadAllMenu();
  },
  methods: {
    // 选择图标
    selected(name) {
      this.form.icon = name;
    },
    //加载所有菜单数据
    loadAllMenu() {
      sysMenuApi.listMenuMC().then(res => {
        this.menuList = [];
        const menu = {menuId: 0, menuName: '主类目', children: res.data};
        this.menuList = [menu];
      }).catch(() => {
      })
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      };
    },
    //保存菜单
    add() {
      this.$refs.form.validate(valid => {
        if (valid) {
          let saveData = emptyStringToNull(this.form)
          sysMenuApi.addMenu(saveData).then(res => {
            this.$message.success(res.msg)
            //调用父组件里面绑定的after事件
            this.$emit("after")
            //调用父组件里面绑定的close事件
            this.$emit("close")
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
